/* api */
import { workbenchMessageWebsocketUrl } from '@src/api/WorkbenchApi'
/* enum */
import StorageModuleEnum from '@model/enum/StorageModuleEnum'
import StorageKeyEnum from '@model/enum/StorageKeyEnum'
/* service */
import WebNotificationServiceImpl from '@service/WebNotificationService/impl/WebNotificationServiceImpl'
/* util */
import Log from '@src/util/log'
import { storageGet, storageSet } from '@src/util/storage.ts'
import { isNull, isUndefined } from '@src/util/type'

export default {
  name: 'frame-notification-mixin',
  data() {
    return {
      notification: null,
      showNotificationAnimation: false,
      workbenchMessageWebsocketUrl
    };
  },
  computed: {
    isShowNotificationNum() {
      return this.notificationNum > 0
    },
    notificationNum() {
      try {
        return this.notification.count + this.ConversationNum && (this.notification.count + this.ConversationNum)
      } catch (error) {
        return 0
      }
    },
    notificationNumDisplayString() {
      return this.notificationNum > 99 ? '99+' : this.notificationNum
    },
    notificationClassNames() {
      return {
        'notification-btn-animate': this.isShowNotificationNum && this.showNotificationAnimation,
      }
    }
  },
  watch: {
    showNotificationAnimation(newValue) {
      if (newValue) {
        setTimeout(() => {
          this.showNotificationAnimation = false
        }, 1000)
        setTimeout(() => {
          this.showNotificationAnimation = true
        }, 11000)
      }
    },
    notificationNum(newValue) {
      const isHaveNotification = newValue > 0
      this.setShowNotificationAnimation(isHaveNotification)
    }
  },
  mounted() {
    // this.notification = new WebNotificationServiceImpl({ title: '测试' })
    // this.notification.requestPermission()
    // this.notification.show()
  },
  methods: {
    onNotificationConnected(ws, event) {
      // Log.warn(event, this.onNotificationConnected.name)
    },
    onNotificationDisconnected(ws, event) {
      // Log.warn(event, this.onNotificationDisconnected.name)
    },
    onNotificationError(ws, event) {
      // Log.error(event, this.onNotificationError.name)
    },
    onNotificationMessage(ws, result) {
      
      console.log('onNotificationMessage', result)
      
      const unReadTotalCount = result?.unReadTotalCount
      if (!isNull(unReadTotalCount) && !isUndefined(unReadTotalCount)) {
        this.setShowNotificationAnimation()
        this.notification.count = unReadTotalCount
      }
      
      const globalWebSocketComponent = this.$refs.GlobalWebSocketComponent
      if(this.$refs.notification?.show) {
        this.$refs.notification?.loadData()
      }
      if (globalWebSocketComponent) {
        
        globalWebSocketComponent.websocketOnMessage({
          data: result?.notifyParam || {}
        })
        
      }
      
    },
    setShowNotificationAnimation(show = true) {
      this.showNotificationAnimation = show
    },
    getNotificationNum() {
      this.$refs.notification.checkCalendar();
    },
    /**
     * @description: 保存数据到缓存
     * @param {StorageKeyEnum} key 缓存键名
     * @param {StorageSetType} data 需要保存的数据
     * @return {Promise<void>}
     */
    async saveDataToStorage(data) {
      return await storageSet(StorageKeyEnum.UserNotificationNum, data, StorageModuleEnum.Task)
    },
    /**
     * @description: 从缓存获取数据
     * @param {StorageKeyEnum} key 缓存键名
     * @param {StorageSetType} data 需要获取的数据
     * @return {Promise<T[]>}
    */
    async getDataWithStorage(defaultValue) {
      return storageGet(StorageKeyEnum.UserNotificationNum, defaultValue, StorageModuleEnum.Task)
    }
  }
}